package com.cs.framework.web.context;

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cs.framework.core.context.Application;
import com.cs.framework.core.context.ContextHelper;

/**
 * Web上下文协助者
 * 
 * @author hedongzhou
 * 
 */
public class WebContextHelper extends ContextHelper {

	protected static Log logger = LogFactory.getLog(WebContextHelper.class);

	/**
	 * 创建应用上下文
	 * 
	 * @param context
	 *            Servlet上下文
	 * @param applicationClass
	 *            应用上下文类型
	 * @return
	 */
	public static Application createApplication(ServletContext context, String applicationClass) {
		// 应用配置路径
		String appConfig = context.getInitParameter("appConfig");
		// 模块公共配置路径
		String moduleCommonConfig = context.getInitParameter("moduleCommonConfig");

		logger.info("appConfig: " + appConfig);
		logger.info("moduleCommonConfig: " + moduleCommonConfig);

		// 创建应用上下文
		Application app = createApplication(applicationClass, appConfig, moduleCommonConfig);

		((WebApplication) app).setServletContext(context);

		// 初始化
		app.init();

		return app;
	}

}
